package cn.unknownerror.ueteam.team;

import cn.unknownerror.ueteam.data.Data;

import java.util.*;

public class AbstractTeamMeta implements Meta{

    private Data data;
    private Data child;

    private String name;
    private int maxMember;
    private final UUID captain;
    private List<UUID> members;
    private final UUID uuid;
    private boolean state;

    private ITeam bind;

    AbstractTeamMeta(String _name, int _maxMember, UUID _captain, ITeam bind) throws TeamException {
        name=_name;
        captain=_captain;
        members=new ArrayList<UUID>();
        maxMember=_maxMember;
        members.add(_captain);
        uuid=UUID.randomUUID();
        state=true;

        this.bind=bind;

        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("maxMember",maxMember);
        map.put("captain",captain);
        map.put("members",members);
        map.put("uuid",uuid);
        map.put("state",state);

        data= new Data(map);
    }

    AbstractTeamMeta(Data _data,ITeam _bind) throws TeamException {
        data=new Data();
        data.copy(_data);
        setName(data.getString("name"));
        captain=data.getUUID("captain");
        members=new ArrayList<UUID>();
        setMaxmember(data.getInteger("maxMember"));
        setMembers(data.getUUIDList("members"));
        uuid=data.getUUID("uuid");
        state=data.getBoolean("state");

        bind=_bind;
    }

    public int getMaxmember() {
        return maxMember;
    }

    public void setMaxmember(int i) throws TeamException {
        if(i<1) i=1;
        if(i<members.size()){
            throw new TeamException(bind,"The max number of members was less than the number of players in the current team.");
        }
        else maxMember=i;

        data.put("maxMember",getMaxmember());
    }

    public UUID getCaptain() {
        return captain;
    }

    void removeMember(UUID uuid) {
        members.remove(uuid);

        data.put("members",getMembers());
    }

    public boolean addMember(UUID uuid) {
        if(members.size()>=maxMember) return false;
        if(members.contains(uuid)) return false;
        members.add(uuid);

        data.put("members",getMembers());
        return true;
    }

    public List<UUID> getMembers() {
        return new ArrayList<UUID>(members);
    }

    void setMembers(List<UUID> list) throws TeamException {
        if (list == null) {
            members = new ArrayList<UUID>();
        } else {
            if(list.size()>getMaxmember()) throw new TeamException(bind,"The size of MembersList was more than the max number of members.");
            members = list;
        }

        data.put("members",getMembers());
    }

    public boolean hasMember(UUID uuid) {
        return members.contains(uuid);
    }

    public String getName() {
        return name;
    }

    public void setName(String _name) {
        name=_name;
        data.put("name",getName());
    }

    public UUID getUUID(){
        return uuid;
    }

    public boolean getTeamState() {
        return state;
    }

    public void setTeamState(boolean _state) {
        state=_state;
        data.put("state",state);
    }

    /**
     * @return 返回Meta封装的Data.
     */
    public Data toData(){
        data.put("child",child);
        return data;
    }

    public ITeam getBind(){
        return bind;
    }

    public Data getChildData(){
        return child;
    }

    public void setChildData(Data _child){
        if(_child==null){
            child=new Data();
        }else{
            child=_child;
        }
    }
}
